package com.first.nell.loan

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.first.nell.base.BaseFragment
import com.first.nell.databinding.FragmentFirstNellLoanRejectBinding
import com.first.nell.enums.PageTagEnum
import com.first.nell.extend.getFirstNellScenFirstNellClickFirstNellFlag
import com.first.nell.extend.recordFirstNellEvent
import com.first.nell.extend.startFirstNellToFirstNellNextFirstNellFromFirstNellPage
import com.first.nell.launch.UserStateViewModel

/** 拒绝页 */
class LoanRejectFragment : BaseFragment() {

    //布局binding
    private lateinit var mFragmentBinding: FragmentFirstNellLoanRejectBinding

    //贷款viewModel
    private val mLoanViewModel by lazy {
        ViewModelProvider(this).get(LoanViewModel::class.java)
    }

    //用户流程viewModel
    private val mUserStateViewModel by lazy {
        ViewModelProvider(this).get(UserStateViewModel::class.java)
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel>? {
        val list = mutableListOf<ViewModel>()
        list.add(mLoanViewModel)
        list.add(mUserStateViewModel)
        return list
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mFragmentBinding = FragmentFirstNellLoanRejectBinding.inflate(inflater, container, false)
        return mFragmentBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        try {
            //获取下次再借日期回调
            mLoanViewModel.onFirstNellFetchFirstNellNextFirstNellApplyFirstNellDaysFirstNellLiveData.observe(
                viewLifecycleOwner
            ) {
                mFragmentBinding.textLoanRejectApplyAgain.text = it
            }

            //获取用户状态回调
            mUserStateViewModel.onFirstNellFetchFirstNellUserFirstNellStateFirstNellLiveData.observe(
                viewLifecycleOwner
            ) {
                if (it.isSuccess) {
                    try {
                        startFirstNellToFirstNellNextFirstNellFromFirstNellPage(PageTagEnum.reject)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            }

            //点击刷新
            mFragmentBinding.submitLoanReject.setOnClickListener {
                mFragmentBinding.submitLoanReject.recordFirstNellEvent(
                    type = mFragmentBinding.submitLoanReject.getFirstNellScenFirstNellClickFirstNellFlag(
                        PageTagEnum.reject.name.uppercase()
                    ),
                    remark = ""
                )
                //查询用户状态
                mUserStateViewModel.fetchFirstNellUserFirstNellState()
            }

            //获取下次再借日期
            mLoanViewModel.fetchFirstNellNextFirstNellApplyFirstNellDays()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}